<?php

namespace app\api\model;


use app\api\constant\ConfigConst;
use app\api\util\Date;
use think\model\relation\HasOne;

/**
 * 商品评论模型
 */
class GoodsComment extends BaseModel
{
    protected $table = 'fa_store_comment';
    protected $type = [
        'pic_prefix_url' => 'array',
        'pic_urls' => 'array'
    ];
    protected $append = [
        'ago_date',
        'pic_prefix_url',
    ];
    /**
     * 追加完整图片地址
     * @param $value
     * @param $data
     * @return array
     */
    public function getPicPrefixUrlAttr($value, $data): array
    {
        $picUrls = json_decode($data['pic_urls'], true);
        $result = [];
        foreach ($picUrls as $url) {
            $result[] = ConfigConst::APP_URL . $url;
        }
        return $result;
    }

    /**
     * 追加友好的发布时间字段
     * @param $value
     * @param $data
     * @return string
     */
    public function getAgoDateAttr($value, $data): string
    {
        return Date::timeAgo($data['createtime']);
    }

    /**
     * 关联用户
     * @return HasOne
     */
    public function user(): HasOne
    {
        return $this->hasOne(User::class, 'id', 'user_id');
    }
}